package ui;

import domain.Player;
import ui.JDialog.SpecialYearJDialog;
import events.Events;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

import static events.Events.*;
import static service.GameService.choiceDeath;
import static service.GameService.choiceWhichEvent;
import static ui.SelectAttributesJFrame.player;

/**
 * 游戏主界面
 */
public class GameJFrame extends JFrame {
    private int age = 0;
    JButton Onestep = new JButton("品味人生");
    JButton steps = new JButton("快人一步");
    JButton back = new JButton("返回主菜单");


    public GameJFrame() {
        //初始化界面
        initJFrame();
        //在这个界面中添加内容
        initView();
        //添加图片
        initImage();
        //是否可见(写在最后)
        this.setVisible(true);
    }

    private void initImage() {
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon(this.getClass().getResource("/image/RegisterJFrame/背景.png")));
        background.setBounds(0, 0, 603, 680);
        this.getContentPane().add(background);
    }

    /**
     * 负责人:小橙子、景旭
     * 功能:依据选择的属性判断从什么事件集中抽取人生事件
     * 特殊年时接受用户的选择从对应的事件集中抽取事件(弹窗),并判断是否满足触发彩蛋的条件
     * 如果触发,则执行对应的彩蛋逻辑
     * 设置一个文本框用于打印事件
     * 添加一个返回主菜单按钮,点击回到主菜单
     * 添加品味人生,快人一步两个按钮
     * 点击快人一步则只在特殊年进行选择时停止输出事件,点击品味人生则按下任意键才输出下一年事件
     * <p>
     * 抽取人生事件的逻辑:
     * <p>
     * 特殊年抽取事件的逻辑:
     * <p>
     * 触发彩蛋的逻辑:
     * <p>
     * 点击按钮的逻辑:
     * <p>
     * 打印事件的逻辑:
     */
    private void initView() {
        Onestep.setBounds(30, 600, 120, 40);
        Onestep.setBackground(new Color(253, 222, 122));
        this.add(Onestep);
        steps.setBounds(430, 600, 120, 40);
        steps.setBackground(new Color(253, 222, 122));
        this.add(steps);
        back.setBounds(20, 10, 120, 30);
        back.setBackground(new Color(253, 222, 122));
        this.add(back);

        JTextArea jTextArea = new JTextArea();

        jTextArea.setWrapStyleWord(true);
        jTextArea.setLineWrap(true);

        jTextArea.setBounds(50, 50, 500, 525);
        JScrollPane jScrollPane = new JScrollPane(jTextArea);

        jScrollPane.setBounds(50, 50, 500, 525);
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        this.add(jScrollPane);

        back.addActionListener(e -> {
            Utils.Music.playSound("src\\music\\02.wav");
            new GameMenuJFrame();
            this.setVisible(false);
        });
        Onestep.addActionListener(e -> {
            Utils.Music.playSound("src\\music\\02.wav");
            //品味人生
            if (player.getDeathAge()==0){
                Utils.JDialog.showJDialog("第0岁你死了。当时你出生于职高的厕所，本以为听着优美的下课铃声觉得这辈子会很不错,结果还没哭出来就被冲进了下水道");
                new SettlementJFrame();
                this.dispose();
            }
            else {
                if (age <= player.getDeathAge()) {
                    if (age == 6 || age == 12 || age == 18 || age == 25 || age == 30 || age == 40 || age == 50 || age == 60 || age == 70 || age == 80) {
                        switch (age) {
                            case 6:
                                String chosen6 = SpecialYearJDialog.showSixYearEvent(player);
                                jTextArea.append(player.getName() + "第6岁幼儿园老师发现你擅长拼图：" + chosen6 + "\n");
                                break;
                            case 12:
                                String chosen12 = SpecialYearJDialog.showTwelveYearEvent(player);
                                jTextArea.append(player.getName() + "第12岁初中分班时面临选择：" + chosen12 + "\n");
                                break;
                            case 18:
                                String chosen18 = SpecialYearJDialog.showEighteenYearEvent(player);
                                jTextArea.append(player.getName() + "第18岁高考志愿填报：" + chosen18 + "\n");
                                break;
                            case 25:
                                String chosen25 = SpecialYearJDialog.showTwentyFiveYearEvent(player);
                                jTextArea.append(player.getName() + "第25岁职场晋升机会：" + chosen25 + "\n");
                                break;
                            case 30:
                                String chosen30 = SpecialYearJDialog.showThirtyYearEvent(player);
                                jTextArea.append(player.getName() + "第30岁生育决策：" + chosen30 + "\n");
                                break;
                            case 40:
                                String chosen40 = SpecialYearJDialog.showFortyYearEvent(player);
                                jTextArea.append(player.getName() + "第40岁中年资产规划：" + chosen40 + "\n");
                                break;
                            case 50:
                                String chosen50 = SpecialYearJDialog.showFiftyYearEvent(player);
                                jTextArea.append(player.getName() + "第50岁健康转折点：" + chosen50 + "\n");
                                break;
                            case 60:
                                String chosen60 = SpecialYearJDialog.showSixtyYearEvent(player);
                                jTextArea.append(player.getName() + "第60岁退休生活选择：" + chosen60 + "\n");
                                break;
                            case 70:
                                String chosen70 = SpecialYearJDialog.showSeventyYearEvent(player);
                                jTextArea.append(player.getName() + "第70岁老年兴趣抉择：" + chosen70 + "\n");
                                break;
                            case 80:
                                String chosen80 = SpecialYearJDialog.showEightyYearEvent(player);
                                jTextArea.append(player.getName() + "第80岁遗产分配思考：" + chosen80 + "\n");
                                break;
                        }
                    } else {
                        jTextArea.append(player.getName() + "第" + age + "岁" + choicEvent(age, player));
                        jTextArea.append("\n");
                    }
                    age++;
                } else {
                    Utils.JDialog.showJDialog("第" + (age - 1) + "岁" + player.getName() +
                            "死了。死因:" + choiceDeath());
                    new SettlementJFrame();
                    this.dispose();
                }
            }
        });
        steps.addActionListener(e -> {
            Utils.Music.playSound("src\\music\\02.wav");
            if (player.getDeathAge()==0){
                Utils.JDialog.showJDialog("第0岁你死了。当时你出生于职高的厕所，本以为听着优美的下课铃声觉得这辈子会很不错,结果还没哭出来就被冲进了下水道");
                new SettlementJFrame();
                this.dispose();
            }
            else {
                //快人一步
                int age = 0;
                while (true) {
                    if (age <= player.getDeathAge()) {
                        if (age == 6 || age == 12 || age == 18 || age == 25 || age == 30 || age == 40 || age == 50 || age == 60 || age == 70 || age == 80) {
                            switch (age) {
                                case 6:
                                    String chosen6 = SpecialYearJDialog.showSixYearEvent(player);
                                    jTextArea.append(player.getName() + "第6岁幼儿园老师发现你擅长拼图：" + chosen6 + "\n");
                                    break;
                                case 12:
                                    String chosen12 = SpecialYearJDialog.showTwelveYearEvent(player);
                                    jTextArea.append(player.getName() + "第12岁初中分班时面临选择：" + chosen12 + "\n");
                                    break;
                                case 18:
                                    String chosen18 = SpecialYearJDialog.showEighteenYearEvent(player);
                                    jTextArea.append(player.getName() + "第18岁高考志愿填报：" + chosen18 + "\n");
                                    break;
                                case 25:
                                    String chosen25 = SpecialYearJDialog.showTwentyFiveYearEvent(player);
                                    jTextArea.append(player.getName() + "第25岁职场晋升机会：" + chosen25 + "\n");
                                    break;
                                case 30:
                                    String chosen30 = SpecialYearJDialog.showThirtyYearEvent(player);
                                    jTextArea.append(player.getName() + "第30岁生育决策：" + chosen30 + "\n");
                                    break;
                                case 40:
                                    String chosen40 = SpecialYearJDialog.showFortyYearEvent(player);
                                    jTextArea.append(player.getName() + "第40岁中年资产规划：" + chosen40 + "\n");
                                    break;
                                case 50:
                                    String chosen50 = SpecialYearJDialog.showFiftyYearEvent(player);
                                    jTextArea.append(player.getName() + "第50岁健康转折点：" + chosen50 + "\n");
                                    break;
                                case 60:
                                    String chosen60 = SpecialYearJDialog.showSixtyYearEvent(player);
                                    jTextArea.append(player.getName() + "第60岁退休生活选择：" + chosen60 + "\n");
                                    break;
                                case 70:
                                    String chosen70 = SpecialYearJDialog.showSeventyYearEvent(player);
                                    jTextArea.append(player.getName() + "第70岁老年兴趣抉择：" + chosen70 + "\n");
                                    break;
                                case 80:
                                    String chosen80 = SpecialYearJDialog.showEightyYearEvent(player);
                                    jTextArea.append(player.getName() + "第80岁遗产分配思考：" + chosen80 + "\n");
                                    break;
                            }
                        } else {
                            jTextArea.append(player.getName() + "第" + age + "岁" + choicEvent(age, player));
                            jTextArea.append("\n");
                        }
                        age++;
                    } else {

                        Utils.JDialog.showJDialog("第" + (age - 1) + "岁" + player.getName() +
                                "死了。死因:" + choiceDeath());
                        new SettlementJFrame();
                        this.dispose();
                        break;
                    }

                }
            }
        });


    }

    public String choicEvent(int age, Player player) {
        int i = choiceWhichEvent(player);
        Random rand = new Random();
        int j = rand.nextInt(3);
        switch (i) {
            case 0:
                //返回低iq事件
                return lowIqEvents[age][j];
            case 10:
                //返回低pf事件
                return lowPfEvents[age][j];
            case 20:
                //返回低fs事件
                return lowFsEvents[age][j];
            case 30:
                //返回低ffs事件
                return lowFfsEvents[age][j];
            case 40:
                //返回低luck事件
                return lowLuckEvents[age][j];
            case 100:
                //返回高iq事件
                return highIqEvents[age][j];
            case 1:
                //返回高pf事件
                return highPfEvents[age][j];
            case 2:
                //返回高fs事件
                return highFsEvents[age][j];
            case 3:
                //返回高ffs事件
                return highFfsEvents[age][j];
            case 4:
                //返回高luck事件
                return highLuckEvents[age][j];
        }
        return ordinaryEvents[age][j];
    }

    private void initJFrame() {
        //设置长宽(像素)
        this.setSize(603, 680);
        //设置界面的标题
        this.setTitle("人生重开模拟器 v1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //取消默认的居中方式,只有取消了才会按照,x y轴的方式添加组件
        this.setLayout(null);

    }



}
